package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);

    private StudentService service = new StudentService();
    public void start() {
        stop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            switch (sc.nextInt()){
                case 1:
                    //添加
                    addStudent();
                    break;
                case 2:
                    //删除
                    deleteStudent();
                    break;
                case 3:
                    //修改
                    updateStudent();
                    break;
                case 4:
                    //查看
                    findAllStudent();
                    break;
                case 5:
                    //退出
                    break stop;
                    default:
                        System.out.println("输入错误，请重新输入！");
            }
        }
    }

    //修改
    public void updateStudent() {
        //判断这个ID是否在数组中
        String upId = judge();
        Student stu = inputStudent(upId);
        boolean flag = service.updateStudent(upId,stu);
        if (flag) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

    }

    //删除
    public void deleteStudent() {
        //判断这个ID是否在数组中
        String delId = judge();
        //调用Service中的方法删除
        boolean flag = service.deleteStudent(delId);
        if (flag) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }

    }

    //查看
    public void findAllStudent() {
        //返回null或者数组
        Student[] stus = service.findAllStudent();
        if (stus==null){
            System.out.println("无数据，请先添加学生！");
            return;
        }

        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu!=null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t"+stu.getBirthday());
            }
        }
    }

    //添加
    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生id：");
            id = sc.next();
            if (!service.isExists(id)){//判断当前输入的id是否和数组中的数据有重复
                break;
            }
            System.out.println("学号重复,请重新输入：");
        }
        //输入姓名、年龄和生日的方法
        Student stu = inputStudent(id);
        boolean flag = service.addStudent(stu);//将封装好的学生对象交给service层去完成添加操作
        if (flag) {//根据返回值提示添加成功或失败
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    //封装
    public Student inputStudent(String id){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }

    //判断
    public String judge(){
        String id;
        while (true) {
            System.out.println("输入学生id：");
            id = sc.next();
            //判断：id是否存在，存在的话在哪个位置？
            if (!service.isExists(id)) {
                System.out.println("查无此id，请重新输入");
            }else {
                break;
            }
        }
        return id;
    }
    
}
